#!/usr/bin/env python
# -*- coding:utf-8 -*-
import uuid
from datetime import datetime, date


class MemberObj:

    MEM_STATUS = {
        "INUSE": "MEM-001",
        "STOP": "MEM-002",
        "RES": "MEM-003",
        "DROP": "MEM-009"
    }

    col_keys = ["MEMBER_ID", "MEMBER_CODE", "MEMBER_SNO", "NAME", "UNAME", "PASSWORD", "SEX", "BIRTHDAY", "MOBILE", "MP", "WECHAT",
                "MSN", "IS_AGENT", "PROVICE", "CITY", "REGION", "PROVICE_CODE", "CITY_CODE", "REGION_CODE",
                "REGISTER_TYPE", "LOGIN_COUNT", "REG_IP", "REMARK", "MEMBER_TYPE", "HEAD_PORT", "STATUS",
                "STATUS_RESON", "END_TIME", "EMAIL", "FIRST_LOGINTIME", "LAST_LOGINTIME", "EXP_TIME", "CREATE_DATE",
                "CREATE_USER", "UPDATE_DATE", "UPDATE_USER", "REGISTER_TIME", "UPGRADE_TYPE", "UPGRADE_TIME",
                "UPGRADE_FLAG", "ACCOUNT_TYPE", "CONTRACT_NO", "NEW_USER", "LEVEL", "LEVEL_NAME", "GROW_VALUE",
                "ACCT_POINT", "USED_POINT", "EFFECT_POINT", "INVALID_POINT"]
    col_def_val = ["", "", "", "", "", "", "", None, "", "", "", "", "", "", "", "", "", "", "", "", float(0), "", "", "",
                   "", "", "", None, "", None, None, None, None, "", None, "", None, "", None, "", "", "", "", "", "",
                   float(0), float(0), float(0), float(0), float(0)]

    MEMBER_ID: ""
    MEMBER_CODE: ""
    MEMBER_SNO: ""
    NAME: ""
    UNAME: ""
    PASSWORD: ""
    SEX: ""
    BIRTHDAY: None
    MOBILE: ""
    MP: ""
    WECHAT: ""
    MSN: ""
    IS_AGENT: ""
    PROVICE: ""
    CITY: ""
    REGION: ""
    PROVICE_CODE: ""
    CITY_CODE: ""
    REGION_CODE: ""
    REGISTER_TYPE: ""
    LOGIN_COUNT: float(0)
    REG_IP: ""
    REMARK: ""
    MEMBER_TYPE: ""
    HEAD_PORT: ""
    STATUS: ""
    STATUS_RESON: ""
    END_TIME: None
    EMAIL: ""
    FIRST_LOGINTIME: None
    LAST_LOGINTIME: None
    EXP_TIME: None
    CREATE_DATE: None
    CREATE_USER: ""
    UPDATE_DATE: None
    UPDATE_USER: ""
    REGISTER_TIME: None
    UPGRADE_TYPE: ""
    UPGRADE_TIME: None
    UPGRADE_FLAG: ""
    ACCOUNT_TYPE: ""
    CONTRACT_NO: ""
    NEW_USER: ""
    LEVEL: ""
    LEVEL_NAME: ""
    GROW_VALUE: float(0)
    ACCT_POINT: float(0)
    USED_POINT: float(0)
    EFFECT_POINT: float(0)
    INVALID_POINT: float(0)

    def __init__(self):
        return

    @staticmethod
    def make_reg_data(data):
        for item_index in range(len(MemberObj.col_keys)):
            try:
                _check_val = data[str(MemberObj.col_keys[item_index]).lower()]
                if (isinstance(_check_val, int) or isinstance(_check_val, float)):
                    if _check_val is None:
                        _val = MemberObj.col_def_val[item_index]
                    else:
                        if isinstance(_check_val, int):
                            _val = int(_check_val)
                        elif isinstance(_check_val, float):
                            _val = float(_check_val)
                        else:
                            _val = round(_check_val, 2)
                else:
                    if _check_val is not None and len(_check_val) > 0:
                        _val = data[str(MemberObj.col_keys[item_index]).lower()]  # 这里获取页面数值
                    else:
                        _val = MemberObj.col_def_val[item_index]  # 这里配置数据库默认值
            except KeyError as e:
                if str(MemberObj.col_keys[item_index]).lower() == 'birthday' \
                        or str(MemberObj.col_keys[item_index]).lower() == 'end_time' \
                        or str(MemberObj.col_keys[item_index]).lower() == 'first_logintime' \
                        or str(MemberObj.col_keys[item_index]).lower() == 'last_logintime' \
                        or str(MemberObj.col_keys[item_index]).lower() == 'exp_time' \
                        or str(MemberObj.col_keys[item_index]).lower() == 'create_date' \
                        or str(MemberObj.col_keys[item_index]).lower() == 'update_date' \
                        or str(MemberObj.col_keys[item_index]).lower() == 'register_time' \
                        or str(MemberObj.col_keys[item_index]).lower() == 'upgrade_time':
                    _val = None
                else:
                    _val = ''
            data[str(MemberObj.col_keys[item_index]).upper()] = _val

    @staticmethod
    def split_params(params):
        import math
        g_param = []
        if params['member_id'] is not None and len(params['member_id']) > 0:
            g_param.append('`MEMBER_ID`=' + "\'" + params['member_id'] + "\'")
        if params['member_code'] is not None and len(params['member_code']) > 0:
            g_param.append('`MEMBER_CODE`=' + "\'" + params['member_code'] + "\'")
        if params['member_sno'] is not None and len(params['member_sno']) > 0:
            g_param.append('`MEMBER_SNO`=' + "\'" + params['member_sno'] + "\'")
        if params['name'] is not None and len(params['name']) > 0:
            g_param.append('`NAME`=' + "\'" + params['name'] + "\'")
        if params['uname'] is not None and len(params['uname']) > 0:
            g_param.append('`UNAME`=' + "\'" + params['uname'] + "\'")
        if params['password'] is not None and len(params['password']) > 0:
            g_param.append('`PASSWORD`=' + "\'" + params['password'] + "\'")
        if params['sex'] is not None and len(params['sex']) > 0:
            g_param.append('`SEX`=' + "\'" + params['sex'] + "\'")
        if params['birthday'] is not None and len(params['birthday']) > 0:
            obj_birthday = MemberObj.str_2_date(params['birthday'])
            g_param.append('`BIRTHDAY`=' + "\'" + str(obj_birthday) + "\'")
        if params['mobile'] is not None and len(params['mobile']) > 0:
            g_param.append('`MOBILE`=' + "\'" + params['mobile'] + "\'")
        if params['mp'] is not None and len(params['mp']) > 0:
            g_param.append('`MP`=' + "\'" + params['mp'] + "\'")
        if params['wechat'] is not None and len(params['wechat']) > 0:
            g_param.append('`WECHAT`=' + "\'" + params['wechat'] + "\'")
        if params['msn'] is not None and len(params['msn']) > 0:
            g_param.append('`MSN`=' + "\'" + params['msn'] + "\'")
        if params['is_agent'] is not None:
            _str_is_agent = 1 if params['is_agent'] else 0
            g_param.append('`IS_AGENT`=' + "\'" + str(_str_is_agent) + "\'")
        if params['provice'] is not None and len(params['provice']) > 0:
            g_param.append('`PROVICE`=' + "\'" + params['provice'] + "\'")
        if params['city'] is not None and len(params['city']) > 0:
            g_param.append('`CITY`=' + "\'" + params['city'] + "\'")
        if params['region'] is not None and len(params['region']) > 0:
            g_param.append('`REGION`=' + "\'" + params['region'] + "\'")
        if params['provice_code'] is not None and len(params['provice_code']) > 0:
            g_param.append('`PROVICE_CODE`=' + "\'" + params['provice_code'] + "\'")
        if params['city_code'] is not None and len(params['city_code']) > 0:
            g_param.append('`CITY_CODE`=' + "\'" + params['city_code'] + "\'")
        if params['region_code'] is not None and len(params['region_code']) > 0:
            g_param.append('`REGION_CODE`=' + "\'" + params['region_code'] + "\'")
        if params['register_type'] is not None and len(params['register_type']) > 0:
            g_param.append('`REGISTER_TYPE`=' + "\'" + params['register_type'] + "\'")
        if params['login_count'] > 0:
            g_param.append('`LOGIN_COUNT`=' + "\'" + params['login_count'] + "\'")
        if params['reg_ip'] is not None and len(params['reg_ip']) > 0:
            g_param.append('`REG_IP`=' + "\'" + params['reg_ip'] + "\'")
        if params['remark'] is not None and len(params['remark']) > 0:
            g_param.append('`REMARK`=' + "\'" + params['remark'] + "\'")
        if params['member_type'] is not None and len(params['member_type']) > 0:
            g_param.append('`MEMBER_TYPE`=' + "\'" + params['member_type'] + "\'")
        if params['head_port'] is not None and len(params['head_port']) > 0:
            g_param.append('`HEAD_PORT`=' + "\'" + params['head_port'] + "\'")
        if params['status'] is not None and len(params['status']) > 0:
            g_param.append('`STATUS`=' + "\'" + params['status'] + "\'")
        if params['status_reson'] is not None and len(params['status_reson']) > 0:
            g_param.append('`STATUS_RESON`=' + "\'" + params['status_reson'] + "\'")
        if params['end_time'] is not None and len(params['end_time']) > 0:
            g_param.append('`END_TIME`=' + "\'" + params['end_time'] + "\'")
        if params['email'] is not None and len(params['email']) > 0:
            g_param.append('`EMAIL`=' + "\'" + params['email'] + "\'")
        if params['first_logintime'] is not None and len(params['first_logintime']) > 0:
            if not params['first_logintime'] == 'None':
                g_param.append('`FIRST_LOGINTIME`=' + "\'" + params['first_logintime'] + "\'")
        if params['last_logintime'] is not None and len(params['last_logintime']) > 0:
            if not params['last_logintime'] == 'None':
                g_param.append('`LAST_LOGINTIME`=' + "\'" + params['last_logintime'] + "\'")
        if params['exp_time'] is not None and len(params['exp_time']) > 0:
            obj_exp_time = MemberObj.str_2_date(params['exp_time'])
            g_param.append('`EXP_TIME`=' + "\'" + str(obj_exp_time) + "\'")
        if params['create_date'] is not None and len(params['create_date']) > 0:
            g_param.append('`CREATE_DATE`=' + "\'" + params['create_date'] + "\'")
        if params['create_user'] is not None and len(params['create_user']) > 0:
            g_param.append('`CREATE_USER`=' + "\'" + params['create_user'] + "\'")
        if params['update_date'] is not None and len(params['update_date']) > 0:
            g_param.append('`UPDATE_DATE`=' + "\'" + params['update_date'] + "\'")
        if params['update_user'] is not None and len(params['update_user']) > 0:
            g_param.append('`UPDATE_USER`=' + "\'" + params['update_user'] + "\'")
        if params['register_time'] is not None and len(params['register_time']) > 0:
            g_param.append('`REGISTER_TIME`=' + "\'" + params['register_time'] + "\'")
        if params['upgrade_type'] is not None and len(params['upgrade_type']) > 0:
            g_param.append('`UPGRADE_TYPE`=' + "\'" + params['upgrade_type'] + "\'")
        if params['upgrade_time'] is not None and len(params['upgrade_time']) > 0:
            g_param.append('`UPGRADE_TIME`=' + "\'" + params['upgrade_time'] + "\'")
        if params['upgrade_flag'] is not None and len(params['upgrade_flag']) > 0:
            g_param.append('`UPGRADE_FLAG`=' + "\'" + params['upgrade_flag'] + "\'")
        if params['account_type'] is not None and len(params['account_type']) > 0:
            g_param.append('`ACCOUNT_TYPE`=' + "\'" + params['account_type'] + "\'")
        if params['contract_no'] is not None and len(params['contract_no']) > 0:
            g_param.append('`CONTRACT_NO`=' + "\'" + params['contract_no'] + "\'")
        if params['new_user'] is not None:
            _str_new_user = 1 if params['new_user'] else 0
            g_param.append('`NEW_USER`=' + "\'" + str(_str_new_user) + "\'")
        if params['level'] is not None and len(params['level']) > 0:
            g_param.append('`LEVEL`=' + "\'" + params['level'] + "\'")
        if params['level_name'] is not None and len(params['level_name']) > 0:
            g_param.append('`LEVEL_NAME`=' + "\'" + params['level_name'] + "\'")
        if params['grow_value'] is not None and math.fabs(int(params['grow_value'])) > 0:
            g_param.append('`GROW_VALUE`=' + "\'" + str(params['grow_value']) + "\'")
        if params['acct_point'] is not None and int(params['acct_point']) > 0:
            g_param.append('`ACCT_POINT`=' + "\'" + str(params['acct_point']) + "\'")
        if params['used_point'] is not None and int(params['used_point']) > 0:
            g_param.append('`USED_POINT`=' + "\'" + str(params['used_point']) + "\'")
        if params['effect_point'] is not None and int(params['effect_point']) > 0:
            g_param.append('`EFFECT_POINT`=' + "\'" + str(params['effect_point']) + "\'")
        if params['invalid_point'] is not None and int(params['invalid_point']) > 0:
            g_param.append('`INVALID_POINT`=' + "\'" + str(params['invalid_point']) + "\'")
        return ', '.join(str(i) for i in g_param)

    @staticmethod
    def concat_datas(g_param):
        # 获取当前时间
        current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        if g_param["MEMBER_ID"] is None or len(g_param["MEMBER_ID"]) == 0:
            generated_uuid = str(uuid.uuid4())[:32].replace("-", "")
        else:
            generated_uuid = g_param["MEMBER_ID"]
        arry = [generated_uuid, g_param["MEMBER_CODE"], g_param["MEMBER_SNO"], g_param["NAME"], g_param["UNAME"],
                g_param["PASSWORD"], g_param["SEX"], g_param["BIRTHDAY"], g_param["MOBILE"], g_param["MP"],
                g_param["WECHAT"], g_param["MSN"], g_param["IS_AGENT"], g_param["PROVICE"], g_param["CITY"],
                g_param["REGION"], g_param["PROVICE_CODE"], g_param["CITY_CODE"], g_param["REGION_CODE"],
                g_param["REGISTER_TYPE"], g_param["LOGIN_COUNT"], g_param["REG_IP"], g_param["REMARK"],
                g_param["MEMBER_TYPE"], g_param["HEAD_PORT"], g_param["STATUS"], g_param["STATUS_RESON"],
                g_param["END_TIME"], g_param["EMAIL"], g_param["FIRST_LOGINTIME"], g_param["LAST_LOGINTIME"],
                g_param["EXP_TIME"], current_time, g_param["CREATE_USER"], g_param["UPDATE_DATE"],
                g_param["UPDATE_USER"], current_time, g_param["UPGRADE_TYPE"], g_param["UPGRADE_TIME"],
                g_param["UPGRADE_FLAG"], g_param["ACCOUNT_TYPE"], g_param["CONTRACT_NO"], g_param["NEW_USER"],
                g_param["LEVEL"], g_param["LEVEL_NAME"], g_param["GROW_VALUE"], g_param["ACCT_POINT"],
                g_param["USED_POINT"], g_param["EFFECT_POINT"], g_param["INVALID_POINT"]]
        _res = []
        for _val in arry:
            if isinstance(_val, str):
                _res.append(f"\'{_val}\'")
            else:
                if _val is None:
                    _res.append("null")
                else:
                    _res.append(_val)
        return ', '.join(str(i) for i in _res)

    @staticmethod
    def reg_memeber_id():
        return str(uuid.uuid4())[:32].replace("-", "")

    @staticmethod
    def load_db_2_dict(data):
        g_param = {}
        g_param["member_id"] = data[0]
        g_param["member_code"] = data[1]
        g_param["member_sno"] = data[2]
        g_param["name"] = data[3]
        g_param["uname"] = data[4]
        g_param["password"] = data[5]
        g_param["sex"] = data[6]
        g_param["birthday"] = "" if data[7] is None else str(data[7])
        g_param["mobile"] = data[8]
        g_param["mp"] = data[9]
        g_param["wechat"] = data[10]
        g_param["msn"] = data[11]
        g_param["is_agent"] = data[12]
        g_param["provice"] = data[13]
        g_param["city"] = data[14]
        g_param["region"] = data[15]
        g_param["provice_code"] = data[16]
        g_param["city_code"] = data[17]
        g_param["region_code"] = data[18]
        g_param["register_type"] = data[19]
        g_param["login_count"] = float(data[20])
        g_param["reg_ip"] = data[21]
        g_param["remark"] = data[22]
        g_param["member_type"] = data[23]
        g_param["head_port"] = data[24]
        g_param["status"] = data[25]
        g_param["status_reson"] = data[26]
        g_param["end_time"] = "" if data[27] is None else str(data[27])
        g_param["email"] = data[28]
        g_param["first_logintime"] = str(data[29])
        g_param["last_logintime"] = str(data[30])
        g_param["exp_time"] = "" if data[31] is None else str(data[31])
        g_param["create_date"] = "" if data[32] is None else str(data[32])
        g_param["create_user"] = data[33]
        g_param["update_date"] = "" if data[34] is None else str(data[34])
        g_param["update_user"] = data[35]
        g_param["register_time"] = "" if data[36] is None else str(data[36])
        g_param["upgrade_type"] = data[37]
        g_param["upgrade_time"] = "" if data[38] is None else str(data[38])
        g_param["upgrade_flag"] = data[39]
        g_param["account_type"] = data[40]
        g_param["contract_no"] = data[41]
        g_param["new_user"] = data[42]
        g_param["level"] = data[43]
        g_param["level_name"] = data[44]
        g_param["grow_value"] = float(data[45])
        g_param["acct_point"] = float(data[46])
        g_param["used_point"] = float(data[47])
        g_param["effect_point"] = float(data[48])
        g_param["invalid_point"] = float(data[49])
        return g_param

    @staticmethod
    def to_string(with_flg):
        g_param = []
        if with_flg:
            g_param.append("`mem_member`.`MEMBER_ID`")
            g_param.append("`mem_member`.`MEMBER_CODE`")
            g_param.append("`mem_member`.`MEMBER_SNO`")
            g_param.append("`mem_member`.`NAME`")
            g_param.append("`mem_member`.`UNAME`")
            g_param.append("`mem_member`.`PASSWORD`")
            g_param.append("`mem_member`.`SEX`")
            g_param.append("`mem_member`.`BIRTHDAY`")
            g_param.append("`mem_member`.`MOBILE`")
            g_param.append("`mem_member`.`MP`")
            g_param.append("`mem_member`.`WECHAT`")
            g_param.append("`mem_member`.`MSN`")
            g_param.append("`mem_member`.`IS_AGENT`")
            g_param.append("`mem_member`.`PROVICE`")
            g_param.append("`mem_member`.`CITY`")
            g_param.append("`mem_member`.`REGION`")
            g_param.append("`mem_member`.`PROVICE_CODE`")
            g_param.append("`mem_member`.`CITY_CODE`")
            g_param.append("`mem_member`.`REGION_CODE`")
            g_param.append("`mem_member`.`REGISTER_TYPE`")
            g_param.append("`mem_member`.`LOGIN_COUNT`")
            g_param.append("`mem_member`.`REG_IP`")
            g_param.append("`mem_member`.`REMARK`")
            g_param.append("`mem_member`.`MEMBER_TYPE`")
            g_param.append("`mem_member`.`HEAD_PORT`")
            g_param.append("`mem_member`.`STATUS`")
            g_param.append("`mem_member`.`STATUS_RESON`")
            g_param.append("`mem_member`.`END_TIME`")
            g_param.append("`mem_member`.`EMAIL`")
            g_param.append("`mem_member`.`FIRST_LOGINTIME`")
            g_param.append("`mem_member`.`LAST_LOGINTIME`")
            g_param.append("`mem_member`.`EXP_TIME`")
            g_param.append("`mem_member`.`CREATE_DATE`")
            g_param.append("`mem_member`.`CREATE_USER`")
            g_param.append("`mem_member`.`UPDATE_DATE`")
            g_param.append("`mem_member`.`UPDATE_USER`")
            g_param.append("`mem_member`.`REGISTER_TIME`")
            g_param.append("`mem_member`.`UPGRADE_TYPE`")
            g_param.append("`mem_member`.`UPGRADE_TIME`")
            g_param.append("`mem_member`.`UPGRADE_FLAG`")
            g_param.append("`mem_member`.`ACCOUNT_TYPE`")
            g_param.append("`mem_member`.`CONTRACT_NO`")
            g_param.append("`mem_member`.`NEW_USER`")
            g_param.append("`mem_member`.`LEVEL`")
            g_param.append("`mem_member`.`LEVEL_NAME`")
            g_param.append("`mem_member`.`GROW_VALUE`")
            g_param.append("`mem_member`.`ACCT_POINT`")
            g_param.append("`mem_member`.`USED_POINT`")
            g_param.append("`mem_member`.`EFFECT_POINT`")
            g_param.append("`mem_member`.`INVALID_POINT`")
        else:
            g_param.append("`MEMBER_ID`")
            g_param.append("`MEMBER_CODE`")
            g_param.append("`MEMBER_SNO`")
            g_param.append("`NAME`")
            g_param.append("`UNAME`")
            g_param.append("`PASSWORD`")
            g_param.append("`SEX`")
            g_param.append("`BIRTHDAY`")
            g_param.append("`MOBILE`")
            g_param.append("`MP`")
            g_param.append("`WECHAT`")
            g_param.append("`MSN`")
            g_param.append("`IS_AGENT`")
            g_param.append("`PROVICE`")
            g_param.append("`CITY`")
            g_param.append("`REGION`")
            g_param.append("`PROVICE_CODE`")
            g_param.append("`CITY_CODE`")
            g_param.append("`REGION_CODE`")
            g_param.append("`REGISTER_TYPE`")
            g_param.append("`LOGIN_COUNT`")
            g_param.append("`REG_IP`")
            g_param.append("`REMARK`")
            g_param.append("`MEMBER_TYPE`")
            g_param.append("`HEAD_PORT`")
            g_param.append("`STATUS`")
            g_param.append("`STATUS_RESON`")
            g_param.append("`END_TIME`")
            g_param.append("`EMAIL`")
            g_param.append("`FIRST_LOGINTIME`")
            g_param.append("`LAST_LOGINTIME`")
            g_param.append("`EXP_TIME`")
            g_param.append("`CREATE_DATE`")
            g_param.append("`CREATE_USER`")
            g_param.append("`UPDATE_DATE`")
            g_param.append("`UPDATE_USER`")
            g_param.append("`REGISTER_TIME`")
            g_param.append("`UPGRADE_TYPE`")
            g_param.append("`UPGRADE_TIME`")
            g_param.append("`UPGRADE_FLAG`")
            g_param.append("`ACCOUNT_TYPE`")
            g_param.append("`CONTRACT_NO`")
            g_param.append("`NEW_USER`")
            g_param.append("`LEVEL`")
            g_param.append("`LEVEL_NAME`")
            g_param.append("`GROW_VALUE`")
            g_param.append("`ACCT_POINT`")
            g_param.append("`USED_POINT`")
            g_param.append("`EFFECT_POINT`")
            g_param.append("`INVALID_POINT`")

        return ', '.join(str(i) for i in g_param)

    @staticmethod
    def str_2_date(input_str) -> object:
        try:
            # 定义日期时间格式化字符串
            format_str = '%Y-%m-%dT%H:%M:%S.%fZ'
            # 将字符串按指定格式转换为datetime对象
            dt_obj = datetime.strptime(input_str, format_str)
            # 提取年、月、日信息并构建Date对象
            year = dt_obj.year
            month = dt_obj.month
            day = dt_obj.day
            date_obj = date(year=year, month=month, day=day)
            return date_obj
        except Exception as e:
            return input_str


if __name__ == '__main__':
    for i in range(15):
        print(str(uuid.uuid4())[:32].replace("-", ""))
        print(MemberObj.str_2_date(input_str='2024-01-28T16:00:00.000Z'))
